<template>
  <div class="sidebar">
    <div class="logo-container">
      <img 
        src="@/static/icons/logo.png" 
        alt="Logo" 
        class="logo"
      />
      <h1 class="logo-title">Kitchen</h1>
    </div>

    <nav class="navigation">
      <ul>
        <li 
          v-for="(item, index) in menuData" 
          :key="index"
          class="menu-item"
        >
          <div 
            @click="toggleSubmenu(index)"
            class="menu-item-header"
            :class="{ 'active': isActive(item) }"
          >
            <span class="menu-icon">
              <i :class="item.meta.icon"></i>
            </span>
            <span class="menu-text">{{ item.meta.name }}</span>
            
            <span 
              v-if="item.children" 
              class="submenu-toggle"
            >
              {{ item.isOpen ? '▼' : '▶' }}
            </span>
          </div>

          <transition name="submenu">
            <ul 
              v-if="item.children && item.isOpen" 
              class="submenu"
            >
              <li 
                v-for="(child, childIndex) in item.children" 
                :key="childIndex"
                class="submenu-item"
                @click="handleClick(child)"
                :class="{ 'active': isActive(child) }"
              >
                <i :class="child.meta.icon"></i>
                <span>{{ child.meta.name }}</span>
              </li>
            </ul>
          </transition>
        </li>
      </ul>
    </nav>
  </div>
</template>

<script>
export default {
    name: 'Aside',
    data() {
        return {
            menuData: []
        };
    },
    methods: {
        toggleSubmenu(index) {
            if (this.menuData[index].children) {
                this.menuData[index].isOpen = !this.menuData[index].isOpen;
            } else {
                this.handleClick(this.menuData[index]);
            }
        },
        handleClick(item) {
            if (item.meta && item.meta.path) {
                this.$router.push(item.meta.path);
                this.$store.commit('addMenu', item.meta);
            }
        },
        isActive(item) {
            return this.$route.path === item.meta.path;
        }
    },
    mounted() {
        const mainRoute = this.$router.options.routes.find(route => route.path === '/');
        if (mainRoute && mainRoute.children) {
            this.menuData = mainRoute.children.map(item => ({
                ...item,
                isOpen: false
            }));
        }
    }
}
</script>

<style lang="less" scoped>
.sidebar {
  width: 250px;
  height: 100vh;
  background: linear-gradient(to bottom, #ffffff, #f8f9fa);
  color: #2c3e50;
  transition: all 0.3s ease;
  overflow-y: auto;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.05);
  border-right: 1px solid rgba(0, 0, 0, 0.05);
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 25px 20px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(10px);
}

.logo-container:hover {
  background-color: rgba(77, 171, 247, 0.05);
}

.logo {
  width: 45px;
  height: 45px;
  margin-right: 15px;
  transition: all 0.3s ease;
  border-radius: 12px;
  padding: 5px;
  background: rgba(77, 171, 247, 0.1);
}

.logo-container:hover .logo {
  transform: rotate(15deg) scale(1.1);
  background: rgba(77, 171, 247, 0.2);
}

.logo-title {
  font-size: 22px;
  font-weight: 600;
  color: #2c3e50;
  letter-spacing: 1px;
  transition: all 0.3s ease;
  margin: 0;
}

.logo-container:hover .logo-title {
  color: #4dabf7;
}

.navigation {
  padding: 20px 15px;
}

.menu-item {
  margin-bottom: 8px;
}

.menu-item-header {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #2c3e50;
  border-radius: 12px;
  background: transparent;

  &:hover {
    background-color: rgba(77, 171, 247, 0.05);
    transform: translateX(5px);
  }

  &.active {
    background-color: rgba(77, 171, 247, 0.1);
    color: #4dabf7;
    font-weight: 500;
  }
}

.menu-icon {
  margin-right: 15px;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: rgba(77, 171, 247, 0.05);
  transition: all 0.3s ease;
}

.menu-item-header:hover .menu-icon {
  background: rgba(77, 171, 247, 0.1);
}

.menu-item-header.active .menu-icon {
  background: rgba(77, 171, 247, 0.15);
}

.menu-text {
  flex-grow: 1;
  font-size: 15px;
}

.submenu-toggle {
  font-size: 12px;
  transition: transform 0.3s ease;
  color: #909399;
}

.submenu {
  background-color: rgba(77, 171, 247, 0.02);
  padding: 8px 0;
  margin: 0 5px;
  border-radius: 8px;
}

.submenu-item {
  padding: 10px 15px 10px 45px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  color: #606266;
  border-radius: 8px;
  margin: 0 5px;

  i {
    margin-right: 10px;
    font-size: 16px;
    color: #909399;
    transition: all 0.3s ease;
  }

  &:hover {
    background-color: rgba(77, 171, 247, 0.05);
    color: #4dabf7;
    transform: translateX(5px);

    i {
      color: #4dabf7;
    }
  }

  &.active {
    background-color: rgba(77, 171, 247, 0.1);
    color: #4dabf7;
    font-weight: 500;

    i {
      color: #4dabf7;
    }
  }
}

.submenu-enter-active, 
.submenu-leave-active {
  transition: all 0.3s ease;
  max-height: 300px;
  opacity: 1;
}

.submenu-enter-from, 
.submenu-leave-to {
  max-height: 0;
  opacity: 0;
  padding-top: 0;
  padding-bottom: 0;
}

// 自定义滚动条样式
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.02);
}

::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.1);
  border-radius: 3px;

  &:hover {
    background: rgba(0, 0, 0, 0.2);
  }
}
</style>
